<?php

  /** MySQL database name */
  define( 'DB_NAME', 'host1000494_pub' );

  /** MySQL database username */
  define( 'DB_USER', 'host1000494_pub' );

  /** MySQL database password */
  define( 'DB_PASSWORD', 'pub_qwe' );

  /** MySQL hostname */
  define( 'DB_HOST', 'localhost' );

  /** Database Charset to use in creating database tables. */
  define( 'DB_CHARSET', 'utf8' );

  /** The Database Collate type. Don't change this if in doubt. */
  define( 'DB_COLLATE', '' );

  mysql_connect( DB_HOST, DB_USER, DB_PASSWORD );
  mysql_select_db( DB_NAME );
  mysql_query( "set names " . DB_CHARSET );
  mysql_query( "TRUNCATE TABLE `cont_questions`" );

  require_once '../_testing/Task.class.php';
  $id = 1;
  $module_id = 1;
  $dirname = 'tests';
  foreach ( scandir_files( $dirname ) as $filename ) {
      if ( ($handle = fopen( $dirname . '/' . $filename, "r" )) !== FALSE ) {
          $second = false;
          while ( ($data = fgetcsv( $handle, 1000, "," )) !== FALSE ) {
              if ( !$second ) {
                  $second = true;
                  continue;
              }

              $task = new RealTask( );
              $task->parseArray( $data );
              $task->setId( $id );
              $task->setModuleId( $module_id );
              if ( $task->isCorrectTask() ) {
                  echo $task->getQuestion() . "<br>";
                  $task->save();
              }

              $id += 1;
              ;
          }
          fclose( $handle );
      }
      $module_id += 1;
  }
  /*
    class Test {

    var $data;

    function parse ( $filename ) {
    $this->data = null;
    if ( ($handle = fopen( $filename, "r" )) !== FALSE ) {
    $row = 0;
    while ( ($data = fgetcsv( $handle, 1000, "," )) !== FALSE ) {
    if ( $row++ == 0 )
    continue;
    $id = $row - 1;
    $task = new Task( $data, $id );
    if ( $task->isCorrect() )
    $this->data[$id] = $task;
    }
    fclose( $handle );
    }
    }

    function getId ( $id ) {

    }

    function getRandom ( $number = 20 ) {
    foreach ( $this->data as $id => $val ) {
    $ids[] = $id;
    }
    shuffle( $ids );
    return array_slice( $ids, 0, $number );
    }

    function getByIds ( $ids ) {
    foreach ( $ids as $id ) {
    $ret[] = $this->data[$id];
    }
    return $ret;
    }

    function setAnswers ( $answers ) {
    $max_rate = 0.01;
    foreach ( $answers as $id => $answer ) {
    if ( $this->data[$id] == null )
    continue;
    $rate += $this->data[$id]->rateAnswer( $answer );
    $max_rate += $this->data[$id]->getRate();
    }
    $part = round( $rate / $max_rate * 100 );
    $cash = round( $rate / $max_rate * 15000 );
    ?>
    <div class="Result" style="font-size: 300%;">
    Ваш результат:
    <span class="part">
    <?php echo $part ?>%
    </span>
    <span class="cash">
    ~<?php echo $cash ?>руб
    </span>
    </div>
    <?php
    }

    } */
?>
